
import Cookies from 'js-cookie'
import API from '@/api'
import Vue from 'vue'
import { message as Message } from "ant-design-vue"
let pending = false
// const showLogout = function(app) {
//   Vue.prototype.$warning({
//     title: '登录已过期请重新登录',
//     okText: '重新登录',
//     maskClosable: false,
//     closable: false,
//     onOk() {
//       pending = false
//       window.location.replace( app.$sketon.url.loginHost + `/login?clientId=${app.$sketon.auth.clientId}`
//         + '&callBackUrl=' + window.location.href)
//     },
//     class: 'test'
//   })
// }

export default function({ app, $axios, stroe, redirect, error }, inject) {
  inject('api', API($axios))
  $axios.defaults.timeout = 30 * 1000 // 如果请求话费了超过 `timeout` 的时间，请求将被中断
  // $axios.defaults.baseURL = '/gsc01'
  $axios.defaults.baseURL = '/yly-common'
  app.context.globalToken = 'bearer ' + '22ab2d0d-b7c4-456f-a3a2-e87c3a14edba'
  $axios.onRequest(config => {
    // 接口请求前拦截
    // 让每个请求携带 token 请根据实际情况自行修改
    // if (!Cookies.get('user_token')) showLogout(app)
    config.headers['Authorization'] = app.context.globalToken
    return config
  })
  $axios.onResponse(response => {
    // 接口请求后拦截
    // dataAxios 是 axios 返回数据中的 data
    const dataAxios = response.data
    let { code, message } = dataAxios
    if (response.config.headers.forceResove) return response
    if (response.config.responseType === 'blob' && response.status === 200) return response.data
    if (code !== 0 && code !== 200) {
      Message.error(message)
      return Promise.reject('')
    }
    return dataAxios
  })
  $axios.onError(err => {
    if (err.response && err.response.status) {
      if (err.response.status === 404) {
        console.log('请求404')
      }
      if (err.response.status === 401) {
        console.log('请求401')
        console.log(err.response)
        // showLogout(app)
      }
      if (err.response.status === 403) {
        error({
          message: '您没有权限操作资源',
          statusCode: 403
        })
        console.log('请求403')
      }
    } else {
      console.log(err)
    }
  })
}
